// SilverlightDesktop.Net - http://www.SilverlightDesktop.net
// Copyright (c) 2008
// by SilverlightDesktop.Net
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Web.Services;
using System.Web.Script.Services;

using System.Collections.Generic;
using SilverlightDesktopCore.DAL;
using System.Linq;

[WebService(Namespace = "http://SilverlightDesktop.net/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public partial class WebService : System.Web.Services.WebService
{
    #region HelloUser
    [WebMethod(Description = "HelloUser")]
    [ScriptMethod()]
    public string HelloUser(int PortalID, int ModuleId, int UserID, string Password)
    {
        string strIPAddress = this.Context.Request.UserHostAddress;
        SilverlightDesktopAuthendicationHeader SilverlightDesktopAuthendicationHeader = new SilverlightDesktopAuthendicationHeader();
        SilverlightDesktopAuthendicationHeader.PortalID = PortalID;
        SilverlightDesktopAuthendicationHeader.UserID = UserID;
        SilverlightDesktopAuthendicationHeader.Password = Password;
        SilverlightDesktopAuthendicationHeader.ModuleId = ModuleId;
        SilverlightDesktopAuthendicationHeader.IPAddress = strIPAddress;

        string response = "";

        Authendication Authendication = new Authendication(SilverlightDesktopAuthendicationHeader);
        if (Authendication.IsUserValid())
        {
            UserInfo objUser = Authendication.GetUserInfo();
            response = String.Format("Hello {0}! [{1}]", objUser.DisplayName, strIPAddress);
        }
        else
        {
            response = String.Format("Hello SilverlightDesktop Visitor! [{0}]", strIPAddress);
        }

        return response;
    }
    #endregion

    #region Logout
    [WebMethod(Description = "Logout")]
    [ScriptMethod()]
    public void Logout(int PortalID, int ModuleId, int UserID, string Password)
    {
        string strIPAddress = this.Context.Request.UserHostAddress;
        SilverlightDesktopAuthendicationHeader SilverlightDesktopAuthendicationHeader = new SilverlightDesktopAuthendicationHeader();
        SilverlightDesktopAuthendicationHeader.PortalID = PortalID;
        SilverlightDesktopAuthendicationHeader.UserID = UserID;
        SilverlightDesktopAuthendicationHeader.Password = Password;
        SilverlightDesktopAuthendicationHeader.ModuleId = ModuleId;
        SilverlightDesktopAuthendicationHeader.IPAddress = strIPAddress;

        Authendication Authendication = new Authendication(SilverlightDesktopAuthendicationHeader);
        if (Authendication.IsUserValid())
        {
            Authendication.SetSilverlightKey();
        }
    }
    #endregion

    #region GetDesktopModules
    [WebMethod(Description = "GetDesktopModules")]
    [ScriptMethod()]
    public List<SilverlightDesktopModule> GetDesktopModules(int PortalID, int ModuleId, int UserID, string Password)
    {
        string strIPAddress = this.Context.Request.UserHostAddress;
        SilverlightDesktopAuthendicationHeader SilverlightDesktopAuthendicationHeader = new SilverlightDesktopAuthendicationHeader();
        SilverlightDesktopAuthendicationHeader.PortalID = PortalID;
        SilverlightDesktopAuthendicationHeader.UserID = UserID;
        SilverlightDesktopAuthendicationHeader.Password = Password;
        SilverlightDesktopAuthendicationHeader.ModuleId = ModuleId;
        SilverlightDesktopAuthendicationHeader.IPAddress = strIPAddress;

        List<SilverlightDesktopModule> colSilverlightDesktopModule = new List<SilverlightDesktopModule>();

        Authendication Authendication = new Authendication(SilverlightDesktopAuthendicationHeader);
        if (Authendication.IsUserValid())
        {
            UserInfo objUser = Authendication.GetUserInfo();
            SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();

            var SilverlightDesktopModules = from DesktopModules in SilverlightDesktopDAL.SilverlightDesktopModules
                                            select DesktopModules;

            foreach (var DesktopModule in SilverlightDesktopModules)
            {
                if (objUser.IsInRole(DesktopModule.Role) || DesktopModule.Role == "Any")
                {
                    SilverlightDesktopModule SilverlightDesktopModule = new SilverlightDesktopModule();

                    SilverlightDesktopModule.AllowMultipleInstances = DesktopModule.AllowMultipleInstances;
                    SilverlightDesktopModule.AssemblyName = DesktopModule.AssemblyName;
                    SilverlightDesktopModule.ClassName = DesktopModule.ClassName;
                    SilverlightDesktopModule.IconName = DesktopModule.IconName;
                    SilverlightDesktopModule.ModuleDescription = DesktopModule.ModuleDescription;
                    SilverlightDesktopModule.ModuleID = DesktopModule.ModuleID;
                    SilverlightDesktopModule.ModuleName = DesktopModule.ModuleName;
                    SilverlightDesktopModule.Role = DesktopModule.Role;
                    SilverlightDesktopModule.WindowSize = DesktopModule.WindowSize;

                    colSilverlightDesktopModule.Add(SilverlightDesktopModule);
                }
            }
        }
        else
        {
            SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();

            var SilverlightDesktopModules = from DesktopModules in SilverlightDesktopDAL.SilverlightDesktopModules
                                            where DesktopModules.Role == "Any"
                                            select DesktopModules;

            foreach (var DesktopModule in SilverlightDesktopModules)
            {
                SilverlightDesktopModule SilverlightDesktopModule = new SilverlightDesktopModule();

                SilverlightDesktopModule.AllowMultipleInstances = DesktopModule.AllowMultipleInstances;
                SilverlightDesktopModule.AssemblyName = DesktopModule.AssemblyName;
                SilverlightDesktopModule.ClassName = DesktopModule.ClassName;
                SilverlightDesktopModule.IconName = DesktopModule.IconName;
                SilverlightDesktopModule.ModuleDescription = DesktopModule.ModuleDescription;
                SilverlightDesktopModule.ModuleID = DesktopModule.ModuleID;
                SilverlightDesktopModule.ModuleName = DesktopModule.ModuleName;
                SilverlightDesktopModule.Role = DesktopModule.Role;
                SilverlightDesktopModule.WindowSize = DesktopModule.WindowSize;

                colSilverlightDesktopModule.Add(SilverlightDesktopModule);
            }
        }

        return colSilverlightDesktopModule;
    }
    #endregion

    #region GetDesktopModule
    [WebMethod(Description = "GetDesktopModule")]
    [ScriptMethod()]
    public SilverlightDesktopModule GetDesktopModule(int DesktopModuleID)
    {
        SilverlightDesktopModule SilverlightDesktopModule = new SilverlightDesktopModule();

        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();

        var SilverlightDesktopModules = (from DesktopModules in SilverlightDesktopDAL.SilverlightDesktopModules
                                         where DesktopModules.ModuleID == DesktopModuleID
                                         select DesktopModules).FirstOrDefault();

        SilverlightDesktopModule.AllowMultipleInstances = SilverlightDesktopModules.AllowMultipleInstances;
        SilverlightDesktopModule.AssemblyName = SilverlightDesktopModules.AssemblyName;
        SilverlightDesktopModule.ClassName = SilverlightDesktopModules.ClassName;
        SilverlightDesktopModule.IconName = SilverlightDesktopModules.IconName;
        SilverlightDesktopModule.ModuleDescription = SilverlightDesktopModules.ModuleDescription;
        SilverlightDesktopModule.ModuleID = SilverlightDesktopModules.ModuleID;
        SilverlightDesktopModule.ModuleName = SilverlightDesktopModules.ModuleName;
        SilverlightDesktopModule.Role = SilverlightDesktopModules.Role;
        SilverlightDesktopModule.WindowSize = SilverlightDesktopModules.WindowSize;

        return SilverlightDesktopModule;
    }
    #endregion

    #region GetDesktopModuleByUser
    [WebMethod(Description = "GetDesktopModuleByUser")]
    [ScriptMethod()]
    public SilverlightDesktopModule GetDesktopModuleByUser(int PortalID, int UserID, int DesktopModuleID)
    {
        Authendication Authendication = new Authendication();
        UserInfo objUser = Authendication.GetUserInfo(PortalID, UserID);

        SilverlightDesktopModule SilverlightDesktopModule = new SilverlightDesktopModule();
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();

        var SilverlightDesktopModules = (from DesktopModules in SilverlightDesktopDAL.SilverlightDesktopModules
                                         where DesktopModules.ModuleID == DesktopModuleID
                                         select DesktopModules).FirstOrDefault();

        // ModuleID was not found
        // The module may have been deleted
        if (SilverlightDesktopModules == null)
        {
            // Set the ModuleID to -1 so the Silverlight control
            // will realize that Module was deleted and that the Toolbar should be shown
            SilverlightDesktopModule.ModuleID = -1;
            return SilverlightDesktopModule;
        }

        // Is user in the correct role to see the module?
        if (!objUser.IsInRole(SilverlightDesktopModules.Role) & !objUser.IsSuperUser)
        {
            // Set the ModuleID to 0 so the Silverlight control
            // will not load it
            SilverlightDesktopModule.ModuleID = 0;
            return SilverlightDesktopModule;
        }

        SilverlightDesktopModule.AllowMultipleInstances = SilverlightDesktopModules.AllowMultipleInstances;
        SilverlightDesktopModule.AssemblyName = SilverlightDesktopModules.AssemblyName;
        SilverlightDesktopModule.ClassName = SilverlightDesktopModules.ClassName;
        SilverlightDesktopModule.IconName = SilverlightDesktopModules.IconName;
        SilverlightDesktopModule.ModuleDescription = SilverlightDesktopModules.ModuleDescription;
        SilverlightDesktopModule.ModuleID = SilverlightDesktopModules.ModuleID;
        SilverlightDesktopModule.ModuleName = SilverlightDesktopModules.ModuleName;
        SilverlightDesktopModule.Role = SilverlightDesktopModules.Role;
        SilverlightDesktopModule.WindowSize = SilverlightDesktopModules.WindowSize;

        return SilverlightDesktopModule;
    }
    #endregion
}
